package com.business.system.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.business.base.action.BaseAction;
import com.business.base.tools.JsonUtil;
import com.business.base.tools.key.PKConfContext;
import com.business.base.tools.key.PKIDTools;
import com.business.system.pojo.TabRepairGjpg;
import com.business.system.pojo.TabRepairWxgs;
import com.business.system.pojo.TabRepairWxlj;
import com.business.system.service.ITabRapairGJPGService;
import com.business.system.service.ITabRapairWXGSService;
import com.business.system.service.ITabRapairWXLJService;
import com.utility.Page;

@Controller
@ParentPackage("struts-default")
@Namespace("/")
public class RepairGJPGAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private TabRepairGjpg repairGJPG;
	private TabRepairWxgs repairWXGS;
	private TabRepairWxlj repairWXLJ;
	@Autowired
	private ITabRapairGJPGService repairGJPGService;
	@Autowired
	private ITabRapairWXGSService repairWXGSService;
	@Autowired
	private ITabRapairWXLJService repairWXLJService;
	private String id;

	@Action(value = "RepairGJPGAction_list")
	public String list() throws Exception {
		this.pageNo = (int) Math.floor((start + limit) / limit);
		if (paramsCode != null && !paramsCode.equals("")) {
			repairGJPG = (TabRepairGjpg) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairGjpg.class);
		} else {
			repairGJPG = new TabRepairGjpg();
		}

		Page<TabRepairGjpg> page = this.repairGJPGService.findListPage(
				this.repairGJPG, this.pageNo, this.limit);
		List<TabRepairGjpg> list = page.getResults();
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + page.getTotalRecord() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_getPKID")
	public String getPKID() throws Exception {
		// 估价派工号
		String pkID = PKIDTools.getSerialID(PKConfContext.NO_Len_Four,
				PKConfContext.RepairGJPGID_DataPath,
				PKConfContext.RepairGJPGID_Suffix);
		String result = "{success:true,pkID:'" + pkID + "'}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_add")
	public String add() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			repairGJPG = (TabRepairGjpg) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairGjpg.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.repairGJPGService.insert(repairGJPG);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_listGS")
	public String listGS() throws Exception {
		this.pageNo = (int) Math.floor((start + limit) / limit);
		if (paramsCode != null && !paramsCode.equals("")) {
			this.repairWXGS = (TabRepairWxgs) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairWxgs.class);
		} else {
			repairWXGS = new TabRepairWxgs();
		}

		List<TabRepairWxgs> list = this.repairWXGSService.findList(repairWXGS);
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + list.size() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_addGS")
	public String addGS() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			if (paramsCode.length() > 2) {
				paramsCode = paramsCode.substring(1, paramsCode.length() - 1);
			}
			this.repairWXGS = (TabRepairWxgs) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairWxgs.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			if (repairWXGS.getId() != null && !"".equals(repairWXGS.getId())) {
				this.repairWXGSService.update(repairWXGS);
			} else {
				this.repairWXGSService.insert(repairWXGS);
			}
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_deleteGS")
	public String deleteGS() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		try {
			this.repairWXGSService.delete(Integer.parseInt(id));
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_listLJ")
	public String listLJ() throws Exception {
		this.pageNo = (int) Math.floor((start + limit) / limit);
		if (paramsCode != null && !paramsCode.equals("")) {
			this.repairWXLJ = (TabRepairWxlj) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairWxlj.class);
		} else {
			repairWXLJ = new TabRepairWxlj();
		}

		List<TabRepairWxlj> list = this.repairWXLJService.findList(repairWXLJ);
		JSONArray array = JSONArray.fromObject(list);
		String result = "{totalCount:" + list.size() + ",root:"
				+ array.toString() + "}";
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_addLJ")
	public String addLJ() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			if (paramsCode.length() > 2) {
				paramsCode = paramsCode.substring(1, paramsCode.length() - 1);
			}
			this.repairWXLJ = (TabRepairWxlj) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairWxlj.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			if (repairWXLJ.getId() != null && !"".equals(repairWXLJ.getId())) {
				this.repairWXLJService.update(repairWXLJ);
			} else {
				this.repairWXLJService.insert(repairWXLJ);
			}
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_deleteLJ")
	public String deleteLJ() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		try {
			this.repairWXLJService.delete(Integer.parseInt(id));
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_edit")
	public String edit() throws Exception {
		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		if (paramsCode != null && !paramsCode.equals("")) {
			repairGJPG = (TabRepairGjpg) JsonUtil.getObject4JsonString(
					paramsCode, TabRepairGjpg.class);
		} else {
			jsonObject.put("success", bool);
			String result = jsonObject.toString();
			this.jsonOut(result);
			return NONE;
		}
		try {
			this.repairGJPGService.update(this.repairGJPG);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	@Action(value = "RepairGJPGAction_delete")
	public String delete() throws Exception {

		JSONObject jsonObject = new JSONObject();
		boolean bool = false;
		try {
			this.repairGJPGService.delete(id);
			bool = true;
		} catch (Exception ex) {
			ex.printStackTrace();
			bool = false;
		}

		jsonObject.put("success", bool);
		String result = jsonObject.toString();
		this.jsonOut(result);
		return NONE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public TabRepairGjpg getRepairGJPG() {
		return repairGJPG;
	}

	public void setRepairGJPG(TabRepairGjpg repairGJPG) {
		this.repairGJPG = repairGJPG;
	}

	public TabRepairWxgs getRepairWXGS() {
		return repairWXGS;
	}

	public void setRepairWXGS(TabRepairWxgs repairWXGS) {
		this.repairWXGS = repairWXGS;
	}

	public TabRepairWxlj getRepairWXLJ() {
		return repairWXLJ;
	}

	public void setRepairWXLJ(TabRepairWxlj repairWXLJ) {
		this.repairWXLJ = repairWXLJ;
	}

}
